package org.aeon.client.view.components.sessionPanel;

import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.components.button.DropdownButtons;
import org.aeon.entity.dto.FriendApplyDto;
import org.aeon.entity.dto.UserDto;
import org.aeon.message.request.FriendAddRequestMessage;
import org.aeon.utils.SimpleRequestUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;

/**
 * @author: MissingAeon
 * @date: 2024/2/21 12:51
 * @description:
 */
public class FriendNotificationsItem extends JPanel {

    private final JLabel userLabel;
    private final JLabel timeLabel;
    private final JLabel attachmentLabel;
    private final DropdownButtons operationButton;

    public FriendNotificationsItem() {
        this.setBackground(new Color(0x93A8A8A8, true));
        // this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        this.setPreferredSize(new Dimension(600, 60));
        this.setOpaque(false);
        this.setLayout(null);

        final JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);


        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.insets = new Insets(5, 5, 0, 5); // 设置组件之间的间距

        userLabel = new JLabel();
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(userLabel, gbc);

        final JLabel notificationLabel = new JLabel();

        gbc.gridx = 1;
        panel.add(notificationLabel, gbc);

        timeLabel = new JLabel();
        gbc.gridx = 2;
        panel.add(timeLabel, gbc);

        attachmentLabel = new JLabel();
        // 设置文本左对齐
        userLabel.setHorizontalAlignment(JLabel.LEFT);
        final Dimension dimension = new Dimension(350, 20);
        SwingUtil.setSize(attachmentLabel, dimension);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        panel.add(attachmentLabel, gbc);
        panel.setBounds(10, 10, 400, 40);

        this.add(panel);

        operationButton = new DropdownButtons(Color.WHITE, "同意", "拒绝", "忽略");
        operationButton.setBounds(480, 15, 100, 30);
        this.add(operationButton);

        SwingUtil.addGetFocusEvent(this);
    }

    public FriendNotificationsItem(UserDto user, FriendApplyDto friendApply) {
        this();
        userLabel.setText(user.getNickname() + "(" + user.getUsername() + ")");
        final String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(friendApply.getCreateTime());
        timeLabel.setText(time);
        attachmentLabel.setText("留言：" + friendApply.getVerifyMsg());
        if (friendApply.getAgree() != 0) {
            operationButton.clickAfterHandler(friendApply.getAgree() - 1);
        } else {
            operationButton.addActionListener(integer -> {
                final FriendAddRequestMessage reqMsg = new FriendAddRequestMessage();
                friendApply.setAgree(integer.byteValue());
                reqMsg.setMessage(friendApply);
                SimpleRequestUtil.send(GlobalContext.getContext(), reqMsg);
            });
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(getBackground());
        g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 15, 15);
        g2d.dispose();
    }
}
